package fr.fermeManager.application.frames;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.WindowConstants;

import fr.fermeManager.application.controllers.ControllerUserSelection;
import fr.fermeManager.application.controllers.MenuController;
import fr.fermeManager.application.utils.AppContext;
import fr.fermeManager.application.views.BasePanel;
import fr.fermeManager.application.views.ViewUserSelection;


public class MainFrame extends JFrame {
	
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    
    private BasePanel basePanel;
    
    private MenuController menuController = new MenuController();
    
	public MainFrame() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/images/tractor4.png")));
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFichier = new JMenu("Fichier");
		menuBar.add(mnFichier);
		
		JMenuItem mntmOuvrir = new JMenuItem("Importer Inactifs");
		mntmOuvrir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuController.importerInactifs();
			}
		});
		mnFichier.add(mntmOuvrir);
		
		JMenuItem mntmSauvegarderSous = new JMenuItem("Exporter Inactifs");
		mnFichier.add(mntmSauvegarderSous);
		
		JMenuItem mntmExporterBddSous = new JMenuItem("Exporter BDD Sous...");
		mnFichier.add(mntmExporterBddSous);
		
		JMenuItem mntmQuitter = new JMenuItem("Quitter");
		mntmQuitter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuController.quitterApplication();
			}
		});
		
		JMenuItem mntmChangerFerme = new JMenuItem("Changer Ferme");
		mntmChangerFerme.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuController.changerFermeSelection();
			}
		});
		mnFichier.add(mntmChangerFerme);
		
		JMenuItem mntmChangerUtilisateur = new JMenuItem("Changer Utilisateur");
		mntmChangerUtilisateur.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuController.changerUtilisateurSelection();
			}
		});
		mnFichier.add(mntmChangerUtilisateur);
		mnFichier.add(mntmQuitter);
		
		JMenu mnGestion = new JMenu("Gestion");
		menuBar.add(mnGestion);
		
		JMenuItem mntmTypesTransactions = new JMenuItem("Types Transactions");
		mntmTypesTransactions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuController.chargerGestionTypesTransactions();
			}
		});
		
		JMenuItem mntmTransactions = new JMenuItem("Transactions");
		mntmTransactions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuController.chargerFermeInformations();
			}
		});
		mnGestion.add(mntmTransactions);
		mnGestion.add(mntmTypesTransactions);
		
		JMenu mnAPropos = new JMenu("?");
		menuBar.add(mnAPropos);
		
		JMenuItem mntmAPropos = new JMenuItem("A propos");
		mnAPropos.add(mntmAPropos);
		getContentPane().setLayout(new GridLayout(1, 1, 0, 0));
		
		//org.jdesktop.beansbinding.util.logging.Logger.getLogger(ELProperty.class.getName()).setLevel(Level.SEVERE);
	}
	
	public void init() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setSize(new Dimension(600, 400));

        setVisible(true);
        setState(Frame.NORMAL);
        
        // Affectation du nouveau controller
        AppContext.setBasePanel(new ViewUserSelection(new ControllerUserSelection()),false);

    }
	
	public void setBasePanel(BasePanel basePanelParam, boolean pack){
		basePanel = basePanelParam;
		
		getContentPane().removeAll();
		getContentPane().add(basePanelParam);
		getContentPane().setVisible(true);
        basePanelParam.setVisible(true);
        setVisible(true);
        
        chargerDonnees();
        
        if(pack)
        	this.pack();
	}
	
	public void packMainFrame(){
		this.pack();
	}
	
	public BasePanel getBasePanel(){
		return basePanel;
	}
	
	public void chargerDonnees(){
		basePanel.getBaseController().loadData();
	}
}
